#include "SDLSprite.h"

SDLSprite::SDLSprite()
	: SDLComponent()
	, x(0), y(0)
{

}

SDLSprite::~SDLSprite()
{
	SDL_FreeSurface(texture);
}

void SDLSprite::Draw()
{
	ApplySurface(x, y, texture, SDLEngine::GetInstance()->GetScreen());
}

SDL_Surface* SDLSprite::LoadImage( std::string filename )
{
	SDL_Surface* loadedImage = 0;
	SDL_Surface* optimizedImage = 0;

	loadedImage = SDL_LoadBMP(filename.c_str());

	if(loadedImage != 0)
	{
		optimizedImage = SDL_DisplayFormat( loadedImage );
		SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 255,0,255));
		SDL_FreeSurface( loadedImage );
	}
	return optimizedImage;
}

void SDLSprite::ApplySurface( int x, int y
				 , SDL_Surface* src
				 , SDL_Surface* dest)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(src, 0, dest, &offset);
}


void SDLSprite::SetPosition(const int x, const int y)
{
	this->x = x;
	this->y = y;

	ApplySurface(x, y, texture, SDLEngine::GetInstance()->GetScreen());
}
